package cn.com.qiancheng.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.com.qiancheng.dto.ExamPaper;
import cn.com.qiancheng.dto.Question;
import cn.com.qiancheng.util.DbUtil;


public class ExamPaperDao {
	public ExamPaper searchExamPaper(int id){
		ExamPaper exam = null;
		try {
			Connection con = DbUtil.openConnection();
			String sql = "SELECT exam_id,exam_name,exam_question FROM t_exam_paper WHERE t_exam_paper.exam_id = ?";
			PreparedStatement stat = con.prepareStatement(sql);
			stat.setInt(1, id);
			ResultSet rs = stat.executeQuery();
			if(rs.next()){
				exam = new ExamPaper();
				exam.setId(rs.getInt("exam_id"));
				exam.setName(rs.getString("exam_name"));
				String[] question = rs.getString("exam_question").split(",");
				List<Question> list = new ArrayList<>();
				for(int i=0;i<question.length;i++){
					list.add(new QuestionDao().sreachQuestion(Integer.parseInt(question[i])));
				}
				exam.setList(list);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				DbUtil.closeConnection();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return exam;
	}
}
